<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Cool Cloud</title>

    <!-- Google Font: Source Sans Pro -->
    <link rel="stylesheet"
          href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css">
    <!-- DataTables -->
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.24/datatables.min.css"/>
    <link rel="stylesheet" type="text/css"
          href="https://cdn.datatables.net/buttons/1.4.2/css/buttons.dataTables.min.css"/>
    <!-- Theme style -->
    <link rel="stylesheet" href="dist/css/adminlte.min.css">
    <!--xcConfirm-->
    <link rel="stylesheet" href="dist/js/xcConfirm/css/xcConfirm.css">
    <!-- bootstrap选择-->
    <link rel="stylesheet"
          href="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.9/dist/css/bootstrap-select.min.css">
    <style>
        .tipmsg {
            background-color: #f39c12;
            position: absolute;
            z-index: 100;
            top: 30px;
            right: 3%;
            padding: 5px 5px 5px 5px;
            border-radius: 15px;
            border: 2px solid #ddd;
            box-shadow: 0 15px 15px -5px rgba(0, 0, 0, 0.2);
            color: white;
        }
        .modal-dialog {
            top: 40%;
        }
    </style>
</head>
<body class="hold-transition sidebar-mini">
<!-- wrapper -->
<div class="wrapper">
    <!-- Navbar -->
    <div th:replace="common/header :: header"></div>

    <!-- Main Sidebar Container -->
    <div th:replace="common/menu :: sidebar"></div>

    <!-- Content Wrapper. Contains page content -->
    <div th:replace="content/shareContent :: sharecontent"></div>

    <!-- /Footer -->
    <div th:replace="common/footer :: footer"></div>

    <!-- Control Sidebar -->
    <aside class="control-sidebar control-sidebar-dark"></aside>
</div>


<!--编辑个人信息-->
<div th:replace="common/editInforModel :: editinfo"></div>

<!--分享按钮-->
<button type="button" class="share" data-toggle="modal" data-target="#creatShare" style="display: none"></button>
<div id="creatShare" class="modal fade" style="margin-top: -10%" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">分享编辑</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form id="form">
                    <!-- Single button -->
                    <div class="btn-group group1">
                        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"
                                aria-haspopup="true" aria-expanded="false">
                            是否加密<span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu">
                            <li style="text-align: center"><a href="javascript:opendecrypt()">开启</a></li>
                            <li style="text-align: center"><a href="javascript:closedecrypt()">关闭</a></li>
                        </ul>
                    </div>
                    <h1></h1>
                    <div class="btn-group">
                        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"
                                aria-haspopup="true" aria-expanded="false">
                            是否自动过期<span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu">
                            <li style="text-align: center"><a href="javascript:opendate()">开启</a></li>
                            <li style="text-align: center"><a href="javascript:closedate()">关闭</a></li>
                        </ul>
                        <div class="group2" style="display: none">
                            <select class="selectpicker">
                                <option value="1">五分钟后过期</option>
                                <option value="2">一小时后过期</option>
                                <option value="3">一天后过期</option>
                                <option value="4">一星期后过期</option>
                                <option value="5">一个月后过期</option>
                            </select>
                        </div>
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" onclick="createShare()" class="btn btn-primary">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!-- jQuery -->
<script src="plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/adminlte.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="dist/js/demo.js"></script>
<!-- DataTables -->
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.24/datatables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.4.2/js/dataTables.buttons.min.js"></script>
<!--xcConfirm-->
<script src="dist/js/xcConfirm/js/xcConfirm.js"></script>
<!-- Page specific script -->
<!--bootstrap选择-->
<script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.9/dist/js/bootstrap-select.min.js"></script>
<!--邀请注册-->
<script src="dist/js/invite.js"></script>
<!--搜索-->
<script src="dist/js/search.js"></script>
<script>

    function editMyInfo() {
        $(".userName").val("[[${session.loginUser.userName}]]");
        $(".edit").click();
    }

    function submit() {
        let userName = $(".userName").val();
        let IwtNewPsd = $(".IwtNewPsd").val();
        let IwtOralPsd = $(".IwtOralPsd").val();
        let id = [[${session.loginUser.userId}]];
        console.log(userName, id, IwtNewPsd, IwtOralPsd)
        if (userName == '') {
            alert("用户名不能为空！");
            return;
        }
        if ((IwtNewPsd == '' && IwtOralPsd != '') || (IwtNewPsd != '' && IwtOralPsd == '')) {
            alert("需要修改密码,原密码与新密码均不能为空！");
            return;
        }
        $.ajax({
            url: "UpdateInfo",
            type: "POST",
            data: {
                "id": id,
                "username": userName,
                "IwtNewPsd": IwtNewPsd,
                "IwtOralPsd": IwtOralPsd
            },
            success: function (date) {
                if (date.status == "psdwrong") {
                    alert("原密码错误！")
                } else if (date.status == "success" && IwtNewPsd != '') {
                    $("#logout").click();
                } else if (date.status == "success" && IwtNewPsd == '') {
                    $(".tipmsg").html($('<span  style="margin-right: 25px">  用户名修改成功！</span><i class="fas fa-times" onclick="closeTip()"></i>'));
                    $(".tipmsg").show();
                    $('.close').click();
                    setInterval("location.reload( )", 1000);
                }
            }
        })
    }

    let isopendecrypt = 0;

    function opendecrypt() {
        if (isopendecrypt == 0) {
            $('.group1').append("<input type='text' class='form-control' name='decryptpsd' placeholder='四位加密密码：' onblur='checkLen()'>");
            isopendecrypt++;
        }
    }

    function closedecrypt() {
        if (isopendecrypt == 1) {
            $("input[name='decryptpsd']").remove();
            isopendecrypt--;
        }
    }

    function checkLen() {
        let text = $("input[name='decryptpsd']").val();
        if (text.length > 4) {
            $("input[name='decryptpsd']").val(text.slice(0, 4));
        }
    }

    let isopendateend = 0;

    function opendate() {
        isopendateend++;
        $(".group2").show();
    }

    function closedate() {
        isopendateend--;
        $(".group2").hide();
    }

    function createShare() {
        let fileid = $('#fileid').html();
        let req = "fileId=" + fileid + "&url=" + window.location.href.split("coolcloud")[0] + "coolcloud/";
        //开启时间过期
        if (isopendateend == 1) {
            req += "&isOpenEnddate=" + isopendateend + "&endDateType=" + $('.selectpicker').selectpicker().val();
        } else {
            req += "&isOpenEnddate=" + isopendateend;
        }
        //开启加密
        if (isopendecrypt == 1) {
            req += "&isOpenDecrypt=" + isopendecrypt + "&decrypt=" + $("input[name='decryptpsd']").val();
        } else {
            req += "&isOpenDecrypt=" + isopendecrypt;
        }
        $.ajax({
            url: "getQrCode/?" + req,
            type: "get",
            async: false,
            success: function (data) {
                $('.close').click();
                reload();
                let txt = "";
                if (data.password == "unset") {
                    txt = "<img src='" + data['imgPath'] + "' style='width: 150px;height: 150px'/><br>" +
                        "链接：<input style='width: 80%;font-size: 14px' value='" + data['url'] + "'/>";
                } else {
                    txt = "<img src='" + data['imgPath'] + "' style='width: 150px;height: 150px'/><br>" +
                        "密码：<input style='width: 80%;font-size: 14px' value='" + data['password'] + "'/><br>" +
                        "链接：<input style='width: 80%;font-size: 14px' value='" + data['url'] + "'/>";
                }
                var option = {
                    title: "分享你的文件",
                };
                window.wxc.xcConfirm(txt, "custom", option);
            }
        });
    }


    let table = null;
    $(function () {
        $.fn.dataTable.ext.buttons.batchDel = {
            action: function (e, dt, node, config) {
                //获取勾选中的id
                let ids = new Array;
                $("input[name='cb_select']:checked").each(function () {
                    ids.push($(this).val());
                });
                ids = ids.join(",");
                if (ids == "") {
                    return;
                }
                $.ajax({
                    url: "batchDelShare",
                    type: 'POST',
                    data: {ids: ids},
                    success: function (result) {
                        if (result.status == "success") {
                            $(".tipmsg").html($('<span  style="margin-right: 25px">  删除成功！</span><i class="fas fa-times" onclick="closeTip()"></i>'));
                            $(".tipmsg").show();
                            reload();
                        } else {
                            $(".tipmsg").html($('<span  style="margin-right: 25px">  删除失败！请联系管理员</span><i class="fas fa-times" onclick="closeTip()"></i>'));
                            $(".tipmsg").show();
                        }
                        closeTip2sec();
                    }
                });
            }
        };
        table = $('#dateTable').DataTable({

            dom: 'Bfrtip',
            buttons: [
                {
                    extend: 'batchDel',
                    text: '批量删除'
                }
            ],
            pagingType: "simple_numbers",
            language: {
                sSearch: "搜索:",
                sEmptyTable: "这里空空的，什么也找不到",
                info: "显示从_START_到_END_总共_TOTAL_条数据",
                lengthMenu: "默认 _MENU_ 条数据",
                paginate: {
                    next: "下一页",
                    previous: "上一页"
                },
            },
            "ajax": {
                async: 'false',
                url: "getSharedate",
                type: 'GET',
                dataSrc: function (myjson) {
                    return myjson;
                }
            },
            columns: [{
                data: "fileId",
                className: "dt-body-center",
                render: function (data, type, row, meta) {
                    return `<input type="checkbox" name="cb_select" value="${data}">`;
                }
            }, {
                className: "dt-body-center",
                data: "createTime",
            }, {
                className: "dt-body-center",
                data: "isOpenEnddate",
                render: function (data, type, row, meta) {
                    if (data == 0)
                        return "否";
                    else {
                        return "是";
                    }
                }
            }, {
                className: "dt-body-center",
                data: "endDateType",
                render: function (data, type, row, meta) {
                    if (data == 0)
                        return "无";
                    else if (data == 1) {
                        return "5分钟";
                    } else if (data == 2) {
                        return "1小时";
                    } else if (data == 3) {
                        return "1天";
                    } else if (data == 4) {
                        return "1星期";
                    } else {
                        return "1个月";
                    }
                }
            }, {
                className: "dt-body-center",
                data: "isOpenDecrypt",
                render: function (data, type, row, meta) {
                    if (data == 0)
                        return "否";
                    else {
                        return "是";
                    }
                }
            }, {
                className: "dt-body-center",
                data: "fileName",
            }, {
                className: "dt-body-center",
                data: "size",
                render: function (data, type, row, meta) {
                    if (data > 1024)
                        return Math.floor(data / 1024, 2) + "MB";
                    else {
                        return data + "KB";
                    }
                }
            }, {
                data: "fileId",
                className: "dt-body-center",
                render: function (data, type, row, meta) {
                    return "<button type='button' class='btn btn-success btn-sm' onclick='edit(" + data + ")'>编辑</button>&nbsp;<button type='button' class='btn btn-success btn-sm' onclick='remove(" + data + ")'>删除</button>&nbsp;<button type='button' class='btn btn-success btn-sm' onclick='geturl(" + data + ")'>链接</button>";
                }
            }
            ],
        });

    });

    function geturl(id) {
        $.ajax({
            url: "getQrcode",
            type: 'POST',
            data: {
                f: id,
                profixurl: window.location.href.split("coolcloud")[0] + "coolcloud/",
            },
            async: false,
            success: function (data) {
                let txt = "";
                if (data.password == "unset") {
                    txt = "<img src='" + data['imgPath'] + "' style='width: 150px;height: 150px'/><br>" +
                        "链接：<input style='width: 80%;font-size: 14px' value='" + data['url'] + "'/>";
                } else {
                    txt = "<img src='" + data['imgPath'] + "' style='width: 150px;height: 150px'/><br>" +
                        "密码：<input style='width: 80%;font-size: 14px' value='" + data['password'] + "'/><br>" +
                        "链接：<input style='width: 80%;font-size: 14px' value='" + data['url'] + "'/>";
                }
                var option = {
                    title: "分享你的文件",
                };
                window.wxc.xcConfirm(txt, "custom", option);
            }
        })
    }

    function edit(id) {
        $('#fileid').html(id);
        $('.share').click();
    }

    function remove(id) {
        $.ajax({
            url: "batchDelShare",
            type: 'POST',
            data: {ids: id},
            success: function (result) {
                if (result.status == "success") {
                    $(".tipmsg").html($('<span  style="margin-right: 25px">  删除成功！</span><i class="fas fa-times" onclick="closeTip()"></i>'));
                    $(".tipmsg").show();
                    reload();
                } else {
                    $(".tipmsg").html($('<span  style="margin-right: 25px">  删除失败！请联系管理员</span><i class="fas fa-times" onclick="closeTip()"></i>'));
                    $(".tipmsg").show();
                }
                closeTip2sec();
            }
        });
    }

    function reload() {
        table.ajax.reload();
    }

    function closeTip() {
        $(".tipmsg").html("");
        $(".tipmsg").hide();
    }

    function closeTip2sec() {
        setTimeout(function closeTip() {
            $(".tipmsg").html("");
            $(".tipmsg").hide();
        }, 2000);
    }
</script>
</body>
</html>
